<template>
  <div class="audit-container">
    <el-card class="audit-card" shadow="hover">
      <template #header>
        <div class="card-header">
          <h2 class="title">商品审核管理</h2>
          <div class="header-actions">
            <el-button type="primary" @click="refreshData">
              <el-icon>
                <Refresh />
              </el-icon>
              刷新列表
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        :data="productList"
        v-loading="loading"
        style="width: 100%"
        row-key="productId"
      >
        <el-table-column label="商品信息" min-width="300">
          <template #default="{ row }">
            <div class="product-info">
              <el-image
                :src="row.coverImage"
                :preview-src-list="[row.coverImage]"
                fit="cover"
                class="product-image"
              >
                <template #error>
                  <div class="image-error">
                    <el-icon>
                      <Picture />
                    </el-icon>
                  </div>
                </template>
              </el-image>
              <div class="product-detail">
                <h3 class="product-title">{{ row.title }}</h3>
                <p class="product-desc">{{ row.description }}</p>
                <div class="product-meta">
                  <span class="price">¥{{ row.price }}</span>
                  <span class="stock">库存: {{ row.stock }}件</span>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="状态" width="120" align="center">
          <template #default="{ row }">
            <el-tag :type="statusTagType[row.auditStatus]">
              {{ auditStatusMap[row.auditStatus] }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template #default="{ row }">
            <el-button
              type="success"
              size="small"
              @click="handleAudit(row.productId, 1)"
              :disabled="row.auditStatus !== 0"
            >
              通过
            </el-button>
            <el-button
              type="danger"
              size="small"
              @click="handleAudit(row.productId, 2)"
              :disabled="row.auditStatus !== 0"
            >
              拒绝
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-wrapper">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="loadData"
          @size-change="handleSizeChange"
        />
      </div>
    </el-card>

    <!-- 审核确认对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="auditType === 1 ? '通过审核确认' : '拒绝审核确认'"
      width="500px"
    >
      <el-form :model="auditForm" label-width="80px">
        <el-form-item label="审核意见">
          <el-input
            v-model="auditForm.auditComment"
            type="textarea"
            :rows="3"
            placeholder="请输入审核意见（可选）"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmAudit">
          确认{{ auditType === 1 ? "通过" : "拒绝" }}
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { Refresh, Picture } from "@element-plus/icons-vue";
import {
  ProductAuditControllerService,
  ProductControllerService,
} from "@/generated";

// 数据列表
const productList = ref([]);
const loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 审核相关
const dialogVisible = ref(false);
const auditType = ref(1); // 1通过 2拒绝
const auditForm = reactive({
  productId: "",
  auditResult: 1,
  auditComment: "",
});

// 状态映射
const auditStatusMap = {
  0: "待审核",
  1: "已通过",
  2: "已拒绝",
};

const statusTagType = {
  0: "warning",
  1: "success",
  2: "danger",
};

// 加载数据
const loadData = async () => {
  try {
    loading.value = true;
    const res = await ProductControllerService.getProductPage({
      pageNumber: currentPage.value,
      pageSize: pageSize.value,
      auditStatus: 0,
    });
    if (res.code === 0) {
      productList.value = res.data.records;
      total.value = parseInt(res.data.total);
    }
  } catch (error) {
    ElMessage.error("数据加载失败");
  } finally {
    loading.value = false;
  }
};

// 处理分页大小变化
const handleSizeChange = (newSize) => {
  pageSize.value = newSize;
  loadData();
};

// 刷新数据
const refreshData = () => {
  currentPage.value = 1;
  loadData();
};

// 发起审核操作
const handleAudit = (productId, result) => {
  console.log("handleAudit", productId, result);
  auditForm.productId = productId;
  auditForm.auditResult = result;
  auditType.value = result;
  dialogVisible.value = true;
};

// 确认审核
const confirmAudit = async () => {
  try {
    const res = await ProductAuditControllerService.auditProduct(auditForm);
    console.log("confirmAudit", res);
    if (res.code === 0) {
      ElMessage.success("操作成功");
      dialogVisible.value = false;
      await loadData();
    }
  } catch (error) {
    ElMessage.error("操作失败");
  }
};

// 初始化加载
onMounted(() => {
  loadData();
});
</script>

<style scoped lang="scss">
.audit-container {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

.audit-card {
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.title {
  margin: 0;
  color: #1a1a1a;
  font-size: 24px;
  font-weight: 600;
}

.product-info {
  display: flex;
  align-items: center;
  padding: 10px 0;

  .product-image {
    width: 80px;
    height: 80px;
    border-radius: 8px;
    margin-right: 16px;
    flex-shrink: 0;
  }

  .product-detail {
    flex: 1;

    .product-title {
      margin: 0 0 8px;
      font-size: 16px;
      color: #333;
    }

    .product-desc {
      margin: 0 0 8px;
      color: #666;
      font-size: 14px;
    }

    .product-meta {
      display: flex;
      gap: 20px;
      font-size: 14px;

      .price {
        color: #ff6b6b;
        font-weight: 600;
      }

      .stock {
        color: #666;
      }
    }
  }
}

.pagination-wrapper {
  margin-top: 24px;
  display: flex;
  justify-content: flex-end;
}

.image-error {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;

  .el-icon {
    font-size: 24px;
  }
}

:deep(.el-table__row) {
  transition: background-color 0.3s;

  &:hover {
    background-color: #f8f9fa;
  }
}
</style>
